/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.view.setter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Lunayo
 */
public class MultipleChoiceSetterPanel extends javax.swing.JPanel {

    /**
     * Creates new form MultipleChoiceSetter
     */
    public MultipleChoiceSetterPanel() {
        initComponents();
    }
    
    public void setQuestionText(String questionText) {
        this.questionText.setText(questionText);
    }
    
    public String getQuestionText() {
        return this.questionText.getText();
    }
    
    public JTextArea getQuestionTextArea() {
        return this.questionText;
    }
    
    public int getMultipleChoiceCount() {
        return 4;
    }
    
    public JTextField getMultipleChoiceTextArea(int i) {
        String variableName = "multipleChoiceText" + i;
        try {
            Field multipleChoiceField = this.getClass().getDeclaredField(variableName);
            multipleChoiceField.setAccessible(true);
            JTextField field = (JTextField)multipleChoiceField.get(this);
            return field;
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public void setCorrectAnswer(int correctAnswer) {
        if (correctAnswer == 0) return;
        String variableName = "multipleChoiceRadio" + correctAnswer;
        try {
            Field multipleChoiceField = this.getClass().getDeclaredField(variableName);
            multipleChoiceField.setAccessible(true);
            JRadioButton field = (JRadioButton)multipleChoiceField.get(this);    
            field.setSelected(true);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public int getCorrectAnswer() {
        for (int i = 1; i <= multipleChoiceButtonGroup.getButtonCount(); ++i) {
            String variableName = "multipleChoiceRadio" + i;
            try {
                Field multipleChoiceField = this.getClass().getDeclaredField(variableName);
                multipleChoiceField.setAccessible(true);
                JRadioButton field = (JRadioButton)multipleChoiceField.get(this);
                if (field.isSelected()) {
                    return i;
                }
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return 0;
    }
    
    public void setPossibleAnswer(String[] possibleAnswer) {
        int i = 1;
        for (String answer : possibleAnswer) {
            String variableName = "multipleChoiceText" + i;
            try {
                Field multipleChoiceField = this.getClass().getDeclaredField(variableName);
                multipleChoiceField.setAccessible(true);
                JTextField field = (JTextField)multipleChoiceField.get(this);
                field.setText(answer);
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            ++i;
        }
    }
    
    public String[] getPossibleAnswer() {
        ArrayList<String> lines = new ArrayList<String>();
        for (int i = 1; i <= 4; ++i) {
            String variableName = "multipleChoiceText" + i;
            try {
                Field multipleChoiceField = this.getClass().getDeclaredField(variableName);
                multipleChoiceField.setAccessible(true);
                JTextField field = (JTextField)multipleChoiceField.get(this);
                lines.add(field.getText());
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return lines.toArray(new String[lines.size()]);
    }
    
    public void resetInputComponent() {
        // reset all multiple choice text 
        for (int i = 1; i <= 4; ++i) {
            String variableName = "multipleChoiceText" + i;
            try {
                Field multipleChoiceField = this.getClass().getDeclaredField(variableName);
                multipleChoiceField.setAccessible(true);
                JTextField field = (JTextField)multipleChoiceField.get(this);
                field.setText("");
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        // reset all multiple choice radio button
        this.multipleChoiceButtonGroup.clearSelection();
        for (int i = 1; i <= multipleChoiceButtonGroup.getButtonCount(); ++i) {
            String variableName = "multipleChoiceRadio" + i;
            try {
                Field multipleChoiceField = this.getClass().getDeclaredField(variableName);
                multipleChoiceField.setAccessible(true);
                JRadioButton field = (JRadioButton)multipleChoiceField.get(this);
                field.setSelected(false);
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MultipleChoiceSetterPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        // reset question text
        this.questionText.setText("");
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        multipleChoiceButtonGroup = new javax.swing.ButtonGroup();
        questionLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        questionText = new javax.swing.JTextArea();
        possibleAnswerLabel = new javax.swing.JLabel();
        multipleChoiceRadio1 = new javax.swing.JRadioButton();
        multipleChoiceRadio2 = new javax.swing.JRadioButton();
        multipleChoiceRadio3 = new javax.swing.JRadioButton();
        multipleChoiceText1 = new javax.swing.JTextField();
        multipleChoiceText2 = new javax.swing.JTextField();
        multipleChoiceText3 = new javax.swing.JTextField();
        multipleChoiceText4 = new javax.swing.JTextField();
        multipleChoiceRadio4 = new javax.swing.JRadioButton();

        questionLabel.setText("Question :");

        questionText.setColumns(20);
        questionText.setRows(5);
        jScrollPane1.setViewportView(questionText);

        possibleAnswerLabel.setText("Possible Answers :");

        multipleChoiceButtonGroup.add(multipleChoiceRadio1);

        multipleChoiceButtonGroup.add(multipleChoiceRadio2);

        multipleChoiceButtonGroup.add(multipleChoiceRadio3);

        multipleChoiceButtonGroup.add(multipleChoiceRadio4);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(questionLabel)
                            .add(layout.createSequentialGroup()
                                .add(multipleChoiceRadio1)
                                .add(6, 6, 6)
                                .add(multipleChoiceText1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(multipleChoiceRadio2)
                                .add(6, 6, 6)
                                .add(multipleChoiceText2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(multipleChoiceRadio3)
                                .add(6, 6, 6)
                                .add(multipleChoiceText3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(multipleChoiceRadio4)
                                .add(6, 6, 6)
                                .add(multipleChoiceText4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(possibleAnswerLabel))
                        .add(0, 169, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(questionLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(possibleAnswerLabel)
                .add(6, 6, 6)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(multipleChoiceRadio1)
                    .add(multipleChoiceText1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(1, 1, 1)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(multipleChoiceRadio2)
                    .add(multipleChoiceText2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(1, 1, 1)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(multipleChoiceRadio3)
                    .add(multipleChoiceText3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(1, 1, 1)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(multipleChoiceRadio4)
                    .add(multipleChoiceText4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.ButtonGroup multipleChoiceButtonGroup;
    private javax.swing.JRadioButton multipleChoiceRadio1;
    private javax.swing.JRadioButton multipleChoiceRadio2;
    private javax.swing.JRadioButton multipleChoiceRadio3;
    private javax.swing.JRadioButton multipleChoiceRadio4;
    private javax.swing.JTextField multipleChoiceText1;
    private javax.swing.JTextField multipleChoiceText2;
    private javax.swing.JTextField multipleChoiceText3;
    private javax.swing.JTextField multipleChoiceText4;
    private javax.swing.JLabel possibleAnswerLabel;
    private javax.swing.JLabel questionLabel;
    private javax.swing.JTextArea questionText;
    // End of variables declaration//GEN-END:variables

}
